package com.fsxgt.datagrid.executor.controller;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.fsxgt.datagrid.core.exception.DataGridException;
import com.fsxgt.datagrid.core.task.StatusHelper;
import com.fsxgt.datagrid.core.utils.ReturnT;
import com.fsxgt.datagrid.executor.service.DataWorkTaskService;
import com.fsxgt.datagrid.executor.service.FlowTaskService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Api(tags = "执行接口", value = "ExecutorController")
@RequestMapping(produces = "application/json; charset=UTF-8")
@RestController
public class ExecutorController extends BaseController {

	@Autowired
	private FlowTaskService flowTaskService;
	@Autowired
	private DataWorkTaskService dataWorkTaskService;


	@ApiOperation(hidden = true, value = "执行", notes = "")
	@RequestMapping(path = "/executor/start", method = RequestMethod.POST)
	public ReturnT start(@RequestBody JSONObject param) throws Exception {
		
		ReturnT<Map> t=new ReturnT(new HashMap());
		
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		Date triggerTime=format.parse(param.getString("triggerTime"));
		String execId=param.getString("execId");
		String version=param.getString("version");

		Integer threads=param.getInteger("threads");
		String objType=param.getString("objType");
		String objId=param.getString("objId");
		
		Integer timeout=param.getInteger("timeout");
		Integer retryCount=param.getInteger("retryCount");

		

		if(objType.equals("1")) {
			flowTaskService.execute(objId,triggerTime,execId,version,threads,timeout,retryCount);
		}else if(objType.equals("2")) {
			dataWorkTaskService.execute(objId,triggerTime,execId,version,threads,timeout,retryCount);
		}else {
			t.setCode("201");
			t.setMsg("无效任务类型:"+objType);
		}
		
		
		t.getData().put("execId", execId);
		
		return t;

	}
	
	@ApiOperation(hidden = true, value = "停止", notes = "")
	@RequestMapping(path = "/executor/stop", method = RequestMethod.POST)
	public Map stop(@RequestBody JSONObject param) throws DataGridException, IOException {
		Map  result= new HashMap();
		result.put("code", "200");
		
		flowTaskService.interrupted(param.getString("execId"));

		
		return result;

	}
	
	@ApiOperation(hidden = true, value = "获取日志", notes = "")
	@RequestMapping(path = "/executor/getLog", method = RequestMethod.POST)
	public ReturnT getLog(@RequestBody JSONObject param) throws DataGridException, IOException, ParseException {
		

		
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date triggerTime=dateFormat.parse(param.getString("triggerTime"));
		String execId=param.getString("execId");

		String json=JSON.toJSONString(StatusHelper.getLogView(execId,triggerTime));
		
		json=json.replaceAll("SQL", "SQ1").replaceAll("sql", "sq1");
		
		
		return new ReturnT(JSON.parse(json));

	}
	

	


	

}